<?php

namespace common\traits;

use common\helpers\WordMatcher;

/**
 * Description of Constants
 *
 * @author emhome
 */
trait UnifiedKeyword {

    /**
     * @var string 操作内容类型
     */
    public $keyword;

    /**
     * @var string 操作内容类型
     */
    private $_libs = [];

    /**
     * @var string 操作内容类型
     */
    private $_words = [];

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['keyword'], 'validateKeyword'],
        ];
    }

    public function validateKeyword($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $keyword = $this->$attribute;
        if (!$keyword) {
            return;
        }
        $this->_words = explode(' ', $keyword);
        //
        $matched = [];
        $matcher = new WordMatcher();
        $matcher->match($keyword, $matched);
        if (!empty($matched)) {
            $this->_libs = array_keys($matched);
        }
    }

    public function getWords() {
        return $this->_words;
    }

    public function getLibs() {
        return $this->_libs;
    }

}
